import { Matrix3, Matrix4, Vector3, PerspectiveCamera, BoxGeometry, Scene, WebGLRenderer, MeshBasicMaterial, Mesh } from 'three';
export function getModeMatrix(scale: number | Vector3, translate: number | Vector3, rotate: number | Vector3) {
    if (!(scale instanceof Vector3)) {
        scale = new Vector3(scale, scale, scale);
    }
    const scaleMatrix = new Matrix4(

    )
    scaleMatrix.set(
        scale.x, 0, 0, 0,
        0, scale.y, 0, 0,
        0, 0, scale.z, 0,
        0, 0, 0, 1,

    )
    const translateMatrix = new Matrix4()
    if (!(translate instanceof Vector3)) {
        translate = new Vector3(translate, translate, translate);
    }
    translateMatrix.set(
        1, 0, 0, translate.x,
        0, 1, 0, translate.y,
        0, 0, 1, translate.z,
        0, 0, 0, 1,
    )
    // console.log(translateMatrix.transpose())
    const rotateXMatrix = new Matrix4();
    const rotateYMatrix = new Matrix4();
    const rotateZMatrix = new Matrix4();
    if (!(rotate instanceof Vector3)) {
        rotate = new Vector3(rotate, rotate, rotate);
    }
    rotateXMatrix.set(
        1, 0, 0, 0,
        0, Math.cos(rotate.x), -Math.sin(rotate.x), 0,
        0, Math.sin(rotate.x), Math.cos(rotate.x), 0,
        0, 0, 0, 1
    )
    rotateYMatrix.set(
        Math.cos(rotate.y), 0, Math.sin(rotate.y), 0,
        0, 1, 0, 0,
        -Math.sin(rotate.y), 0, Math.cos(rotate.y), 0,
        0, 0, 0, 1
    )
    rotateZMatrix.set(
        Math.cos(rotate.z), -Math.sin(rotate.z), 0, 0,
        Math.sin(rotate.z), Math.cos(rotate.z), 0, 0,
        0, 0, 1, 0,
        0, 0, 0, 1
    )

    // rotalMatrix.multiplyScalar(0.5)
    const m = new Matrix4()
    m
        .multiply(rotateXMatrix)
        .multiply(rotateYMatrix)
        .multiply(rotateZMatrix)
        .multiply(scaleMatrix)
        .multiply(translateMatrix)
    return m;
}



export function getPerspectiveViaOrthographic(
  fov: number, aspect: number, near: number, far: number
): Matrix4 {
  const angle = (fov * Math.PI) / 180;
  const top = near * Math.tan(angle / 2);
  const bottom = -top;
  const right = top * aspect;
  const left = -right;

  // Step 1: Perspective transform (introduces w = -z)
  // 关键改动：把 (3,4) 从 -near*far 改为 +near*far
  const Mpersp = new Matrix4().set(
    near, 0,    0,     0,
    0,    near, 0,     0,
    0,    0,    near + far,  +near * far, // ← 改这里
    0,    0,   -1,     0
  );

  // Step 2: Orthographic normalization to NDC [-1, 1]^3
  // 这里的 z 行采用 (near - far) 的分母与上面的选型配套
  const Mortho = new Matrix4().set(
    2 / (right - left), 0, 0, -(right + left) / (right - left),
    0, 2 / (top - bottom), 0, -(top + bottom) / (top - bottom),
    0, 0, 2 / (near - far),  +(near + far) / (near - far),
    0, 0, 0, 1
  );

  // 最终投影矩阵：P = Mortho × Mpersp
  return Mortho.clone().multiply(Mpersp);
}

export function getProjectMatrix(fov: number, aspect: number, near: number, far: number) {

    const angle = (fov * Math.PI) / 180;
    const t = near * Math.tan(angle / 2);
    const r = t * aspect;
    const l = -r;
    const b = -t;
    const translateMatrix = new Matrix4()
    const scaleMatrix = new Matrix4()
    const projectionMatrix = new Matrix4();
    const m = new Matrix4()
    translateMatrix.set(
        1, 0, 0, -(l + r) / 2,
        0, 1, 0, -(t + b) / 2,
        0, 0, 1, -(near + far) / 2,
        0, 0, 0, 1
    )

    scaleMatrix.set(
        2 / (r - l), 0, 0, 0,
        0, 2 / (t - b), 0, 0,
        0, 0, 2 / ( far-near), 0,
        0, 0, 0, 1
    )
    projectionMatrix.set(
        near, 0, 0, 0,
        0, near, 0, 0,
        0, 0, far + near, -far * near,
        0, 0, 1, 0
    )
    const mt = new Matrix4(

    );
    mt.set(
        1, 0, 0, 0,
        0, 1, 0, 0,
        0, 0, -1, 0,
        0, 0, 0, 1)
    projectionMatrix.multiply(mt)
    m.set(
        1, 0, 0, 0,
        0, 1, 0, 0,
        0, 0, 1, -10,
        0, 0, 0, 1)
    const m1 = scaleMatrix.clone()
        .multiply(translateMatrix).multiply(projectionMatrix)

    return m1;




}
export function getViewMatrix(eye: Vector3) {
    const center = new Vector3(0, 0, 0)
    const up = new Vector3(0, 1, 0);
    const zAxis = eye.clone().sub(center).normalize(); // 👈 方向反了
    const xAxis = up.clone().cross(zAxis).normalize();
    const yAxis = zAxis.clone().cross(xAxis).normalize()
    let viewMatrix = new Matrix4()

    viewMatrix.set(
        xAxis.x, xAxis.y, xAxis.z, -xAxis.dot(eye),
        yAxis.x, yAxis.y, yAxis.z, -yAxis.dot(eye),
        zAxis.x, zAxis.y, zAxis.z, -zAxis.dot(eye),
        0, 0, 0, 1
    );
    // viewMatrix.set(
    //     xAxis.x, xAxis.y, xAxis.z, 0,           // 第1列：xAxis
    //     yAxis.x, yAxis.y, yAxis.z, 0,           // 第2列：yAxis
    //     zAxis.x, zAxis.y, zAxis.z, 0,           // 第3列：zAxis
    //     -xAxis.dot(eye), -yAxis.dot(eye), -zAxis.dot(eye), 1  // 第4列：平移
    // );
    return viewMatrix;
}


// export function getViewMatrix(eye: Vector3) {

//     const center = new Vector3(0, 0, 0);
//     const up = new Vector3(0, 1, 0);

//     // 计算相机朝向的三个基向量
//     const zAxis = eye.clone().sub(center).normalize(); // 摄像机朝向（从目标指向眼睛）
//     const xAxis = up.clone().cross(zAxis).normalize(); // 右向量
//     const yAxis = zAxis.clone().cross(xAxis).normalize(); // 上向量（正交化）

//     const viewMatrix = new Matrix4();

//     // ✅ 按列主序设置矩阵
//     viewMatrix.set(
//         xAxis.x, xAxis.y, xAxis.z, 0,           // 第1列：xAxis
//         yAxis.x, yAxis.y, yAxis.z, 0,           // 第2列：yAxis
//         zAxis.x, zAxis.y, zAxis.z, 0,           // 第3列：zAxis
//         -xAxis.dot(eye), -yAxis.dot(eye), -zAxis.dot(eye), 1  // 第4列：平移
//     );



//     //   console.log(viewMatrix.transpose())

//     // const tranMatrix = new Matrix4();
//     // const m = new Matrix4();
//     // tranMatrix.set(
//     //     1, 0, 0, -eye.x,
//     //     0, 1, 0, -eye.y,
//     //     0, 0, 1, -eye.z,
//     //     0, 0, 0, 1
//     // )
//     // viewMatrix.multiply(tranMatrix)
//     // console.log(viewMatrix)
//     return viewMatrix



// }
/**
 * 角度转换成弧度
 * @param angle 
 * @returns 
 */
export function angle2deg(angle: number) {
    return angle / 180 * Math.PI
}